<script setup lang="ts">
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { provinceAndCityData } from 'element-china-area-data'
import {ElMessageBox} from 'element-plus'
import {onMounted, ref, watch} from "vue";
import {dictKeyToSelect} from "@/common/dict.ts";

const props = defineProps({
  drawerData: {
    type: Object,
    required: true
  },
  drawerConfig: {
    type: Object,
    required: true
  },
  dict: {
    type: Object,
    required: false,
    default: {
      dictIndex: [],
      dictList: []
    }
  }
})



let form = ref({}) //表单
let locale = ref(zhCn)
onMounted(async () => {

})
watch(props.drawerConfig, () => {
  let tmp = {}
  if (props.drawerConfig.form) {
    props.drawerConfig.form.map(item => {
      tmp[item.key] = props.drawerData[item.key]
    })
  }
  form.value = props.drawerData
}, {deep: true})


/**
 * 是否显示该组件，根据配置type和condition条件分别判断
 * @param item
 * @param type
 */
const display = (item, type) => {
  if (item.type === type) {
    if (item.condition) {
      for (let i in item.condition) {
        let con = item.condition[i]
        if (con.type === 'equal' && con.value.indexOf(form.value[con.key]) == -1) return false
        if (con.type === 'noEqual' && con.value.indexOf(form.value[con.key]) != -1) return false
      }
      return true
    } else {
      return true
    }
  } else {
    return false
  }
}
const displayAlert = (item) => {
  if (item.alert) {
    if (item.condition) {
      for (let i in item.condition) {
        let con = item.condition[i]
        if (con.type === 'equal' && con.value.indexOf(form.value[con.key]) == -1) return false
        if (con.type === 'noEqual' && con.value.indexOf(form.value[con.key]) != -1) return false
      }
      return true
    } else {
      return true
    }
  } else {
    return false
  }

}


/**
 * 事件调度中心
 * @param eventName:string 事件名称
 * @param value:object 事件值
 */
const emit = defineEmits(['doit'])
const doit = (eventName: string, value = {}) => {
  emit('doit', {
    eventName: eventName,
    value: value
  })
}
//关闭drawer
const handleClose = () => {
  if (props.drawerConfig.messageBox.status) {
    ElMessageBox.confirm(
        props.drawerConfig.messageBox.message,
        props.drawerConfig.messageBox.title,
        {
          confirmButtonText: props.drawerConfig.messageBox.confirmButtonText,
          cancelButtonText: props.drawerConfig.messageBox.cancelButtonText,
          type: props.drawerConfig.messageBox.type,
        }
    )
        .then(() => {
          doit(props.drawerConfig.messageBox.confirmEventName, props.drawerConfig)
        })
        .catch(() => {
          doit('close')
        })
  } else {
    doit('close')
  }
}
//字典值转select值
const toDictSelect = (dictKey) => {
  return dictKeyToSelect(dictKey, props.dict.dictIndex, props.dict.dictList)
}

let treeName = ""
let treeId = ""
const selectTree = (formName, formId="id") => {
  treeName = formName
  treeId = formId
  console.log(formName, treeId)
}
const handleCheckClick = (e,treeName,treeId) => {
  console.log(e)
  console.log(treeName)
  console.log(form.value[treeName])
  toSelect(e,treeName,treeId)
}

function toSelect(e,treeName,treeId) {
  // console.log(">>>>>>", e, e.id, form.value[treeName].indexOf(e.id), e)
  console.log(e,'选中')
  if (e.children) { //有子元素则不进行判断
    for (let i in e.children) {
      // console.log(i, e[i])
      toSelect(e.children[i],treeName,treeId)
    }
  } else {
      form.value[treeName] = form.value[treeName]||[]
      let index = form.value[treeName].indexOf(e[treeId])
      if (index == -1) {
        if (e[treeId] != null) {
          form.value[treeName].push(e[treeId])
        }
      } else {
        form.value[treeName].splice(index, 1)
      }
  }
}
</script>

<template>

  <el-drawer
      v-model="drawerConfig.show"
      :title="drawerConfig.title"
      :direction="drawerConfig.direction"
      :before-close="handleClose"
      :show-close="true"
      :size="drawerConfig.size"
  >
    <div class="drawer-main">
<!--                  {{drawerData}}-->
      <el-form
          ref="ruleFormRef"
          :model="form"
          :rules="drawerConfig.rules"
          :label-width="drawerConfig.labelWidth"
          status-icon
      >
        <el-space
            fill
            v-for="(item, index) in drawerConfig.form"
            :key="index"
            style="width: 100%"
        >
          <el-alert type="info" show-icon :closable="false" v-if="displayAlert(item)">
            <p class="wos-font-sm">{{ item.alert }}</p>
          </el-alert>

          <!--文本框-->
          <el-form-item
              v-if="display(item, 'text')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-input v-if="drawerConfig.ifSave" v-model="form[item.key]"/>
            <span v-else>{{ form[item.key] }}</span>
          </el-form-item>

          <!--时间-->
          <el-form-item
              v-if="display(item, 'date')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-config-provider :locale="locale">
              <el-date-picker
                  v-if="drawerConfig.ifSave"
                  v-model="form[item.key]"
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="选择日期时间">
              </el-date-picker>
              <span v-else>{{ form[item.key] }}</span>
            </el-config-provider>
            
          </el-form-item>

          <!--密码框-->
          <el-form-item
              v-if="display(item, 'password')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-input type="password" v-if="drawerConfig.ifSave" v-model="form[item.key]"/>
            <span v-else>{{ form[item.key] }}</span>
          </el-form-item>

          <!--文本区域-->
          <el-form-item
              v-if="display(item, 'textarea')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-input
                v-if="drawerConfig.ifSave"
                v-model="form[item.key]"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
            />
            <span v-else>{{ form[item.key] }}</span>
          </el-form-item>

          <!--数字文本框-->
          <el-form-item
              v-if="display(item, 'number')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-input-number
                v-if="drawerConfig.ifSave"
                v-model="form[item.key]"
                :min="item.min"
                :max="item.max"
            />
            <span v-else>{{ form[item.key] }}</span>
          </el-form-item>

          <!--级联选择器-->
          <el-form-item
              v-else-if="display(item, 'treeSelect')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-tree-select
                v-model="form[item.key]"
                :data="item.options"
                check-strictly
                :render-after-expand="false"
                :props="item.props"
                placeholder="默认根目录"
                class="wos-width-100"
                :disabled="!drawerConfig.ifSave"
            />
          </el-form-item>

          <!--级联多项选择器-->
          <el-form-item
              v-else-if="display(item, 'treeSelects')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-tree-select
                v-model="form[item.key]"
                :data="item.options"
                :render-after-expand="false"
                :props="item.props"
                placeholder="默认根目录"
                class="wos-width-100"
                :disabled="!drawerConfig.ifSave"
                multiple
                show-checkbox
            />
          </el-form-item>

          <!--树形菜单-->
          <el-form-item
              v-else-if="display(item, 'tree')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <div class="tree-area">
<!--              {{ item.props.value }}-->
              <el-tree
                  :data="item.options"
                  :props="item.props"
                  :node-key="item.props.value"
                  :default-checked-keys="form[item.key]"
                  show-checkbox
                  @check="handleCheckClick($event,item.key, item.props.value)"
                  @check-change="selectTree(item.key, item.props.value)"
              />
            </div>
          </el-form-item>

          <!--单选按钮-->
          <el-form-item
              v-else-if="display(item, 'radio')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-radio-group v-model="form[item.key]" style="position:relative;top:-3px">
              <el-radio :disabled="!drawerConfig.ifSave" v-if="item.source == 'select'"
                        v-for="option in item.select" :label="option.value" size="large">
                {{ option.label }}
              </el-radio>
<!--  @ts-ignore-->
              <el-radio :disabled="!drawerConfig.ifSave" v-else v-for="option in toDictSelect(item.dict)"
                        :label="option['dictValue']" size="large">{{ option['dictLabel'] }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <!--单选按钮-->
          <el-form-item
              v-else-if="display(item, 'select')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
          <el-select v-model="form[item.key]" placeholder="请选择">
                <el-option
                  v-for="dict in toDictSelect(item.dict)"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
          </el-form-item>
          <el-form-item 
              v-else-if="display(item, 'address')"
              :label="item.label + ':'"
              :prop="item.key"
              :class="drawerConfig.ifSave? '' : 'drawer-input'"
          >
            <el-cascader
              :options="provinceAndCityData"
              change-on-select
              placeholder="请选择"
              v-model="form[item.key]"
              expand-trigger="hover">
            </el-cascader>
          </el-form-item>
        </el-space>

        <el-form-item v-if="drawerConfig.ifSave">
          <el-button type="primary" @click="doit('save', form)">
            保存
          </el-button>
          <el-button @click="doit('close')">放弃</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-drawer>
</template>

<style scoped lang="scss">

</style>